/**
 * @作者 7七月
 * @微信公号 林间有风
 * @开源项目 $ http://7yue.pro
 * @免费专栏 $ http://course.7yue.pro
 * @我的课程 $ http://imooc.com/t/4294850
 * @创建时间 2020-03-15 04:51
 */
package com.clubs.coding.interceptors;

import com.auth0.jwt.interfaces.Claim;
import com.clubs.coding.exception.ImoocMallException;
import com.clubs.coding.exception.ImoocMallExceptionEnum;

import com.clubs.coding.model.dao.UserMapper;
import com.clubs.coding.model.pojo.User;
import com.clubs.coding.utils.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Optional;

public class PermissionInterceptor extends HandlerInterceptorAdapter {
@Autowired
    UserMapper userMapper;
public static  String bearerToken = "";

    public static String getBearerToken() {
        return bearerToken;
    }

    public static void setBearerToken(String bearerToken) {
        PermissionInterceptor.bearerToken = bearerToken;
    }

    public PermissionInterceptor() {
        super();
    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {

        if (!(object instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) object;
        String bearerToken = request.getHeader("Authorization");
        if (StringUtils.isEmpty(bearerToken)) {
            throw new ImoocMallException(ImoocMallExceptionEnum.TOKEN_NO);
        }

        if (!bearerToken.startsWith("Bearer")) {
            throw new ImoocMallException(ImoocMallExceptionEnum.TOKEN_NO);
        }
        String tokens[] = bearerToken.split(" ");

        String token = tokens[1];
        User user = userMapper.selectByToken(token);
        Integer isMember = user.getIsMember();
        if (isMember ==0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.IS_MUBER);
        }
        PermissionInterceptor.setBearerToken(token);
        Optional<Map<String, Claim>> optionalMap = JwtToken.getClaims(token);
        Map<String, Claim> map = optionalMap
                .orElseThrow(() -> {
                    throw new ImoocMallException(ImoocMallExceptionEnum.TOKEN_NO);
                }) ;


        return true;
    }

    //拦截器：请求之中：postHandle
    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse,
                           Object o, ModelAndView modelAndView) throws Exception {

    }

    //拦截器：请求之后：afterCompletion
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse,
                                Object o, Exception e) throws Exception {
    }

}
